package com.newtouch.bxzs.business.personal.service.impl;

import java.util.Date;
import java.util.Objects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.personal.mapper.CustomerBirthdayRemindMapper;
import com.newtouch.bxzs.business.personal.model.AddCustomerBirthdayRemindVO;
import com.newtouch.bxzs.business.personal.model.CustomerBirthdayRemindBO;
import com.newtouch.bxzs.business.personal.model.CustomerBirthdayRemindVO;
import com.newtouch.bxzs.business.personal.service.CustomerBirthdayRemindService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.CustomerBirthdayRemind;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;

/**
 * 客户生日提醒服务接口实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年3月22日</td>
 * <td>xiongdianqiang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author xiongdianqiang
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class CustomerBirthdayRemindServiceImpl implements CustomerBirthdayRemindService {

	/** 静态变量：系统日志 */
	private static final Log logger = LogFactory.getLog(CustomerBirthdayRemindServiceImpl.class);

	@Autowired
	private CustomerBirthdayRemindMapper customerBirthdayRemindMapper;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.newtouch.bxzs.business.personal.service.CustomerBirthdayRemindService
	 * #getCustomerBirthdayRemindList(com.newtouch.bxzs.business.personal.model.
	 * CustomerBirthdayRemindVO)
	 */
	@Override
	public IPage<CustomerBirthdayRemindBO> getCustomerBirthdayRemindList(
			CustomerBirthdayRemindVO customerbirthdayRemindVO) {
		logger.info("获取客户生日提醒列表");
		if (customerbirthdayRemindVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		if (StringUtils.isEmpty(userId)) {
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		Page<CustomerBirthdayRemindBO> page = new Page<CustomerBirthdayRemindBO>();
		page.setCurrent(customerbirthdayRemindVO.getCurrentPage());
		page.setSize(customerbirthdayRemindVO.getPageSize());
		return customerBirthdayRemindMapper.getCustomerBirthdayRemindList(userId, page, customerbirthdayRemindVO);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.newtouch.bxzs.business.personal.service.CustomerBirthdayRemindService
	 * #addCustomerBirthdayRemind(com.newtouch.bxzs.business.personal.model.
	 * AddCustomerBirthdayRemindVO)
	 */
	@Override
	public Integer addCustomerBirthdayRemind(AddCustomerBirthdayRemindVO addCustomerBirthdayRemindVO) {
		logger.info("新建客户生日提醒");
		if (addCustomerBirthdayRemindVO == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		if (StringUtils.isEmpty(userId)) {
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		return customerBirthdayRemindMapper.addCustomerBirthdayRemind(addCustomerBirthdayRemindVO,userId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.newtouch.bxzs.business.personal.service.CustomerBirthdayRemindService
	 * #deleteCustomerBirthdayRemind(java.lang.String)
	 */
	@Override
	public void deleteCustomerBirthdayRemind(String customerBirthdayRemindId) {
		logger.info("删除客户生日提醒");
		//获取登录人id
		String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
		QueryWrapper<CustomerBirthdayRemind> updateWrapper = new QueryWrapper<>();
		updateWrapper.eq("BIRTHDAY_REMIND_ID", customerBirthdayRemindId);
		CustomerBirthdayRemind customerBirthdayRemind = new CustomerBirthdayRemind();
		customerBirthdayRemind.setUpdateTime(new Date());
		customerBirthdayRemind.setUpdateUserId(userId);
		//更新删除时间和删除人
		customerBirthdayRemindMapper.update(customerBirthdayRemind, updateWrapper);
		customerBirthdayRemindMapper.deleteById(customerBirthdayRemindId);
	}
}
